package com.gimi.cloud.jimi.app.consumer.service.userInfo.impl;

import com.gimi.cloud.jimi.app.consumer.feign.userInfo.EmergencyContactFeignService;
import com.gimi.cloud.jimi.app.consumer.service.userInfo.EmergencyContactService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.userInfo.RdEmergencyContactDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class EmergencyContactServiceImpl implements EmergencyContactService {
    private static Logger logger = LoggerFactory.getLogger(EmergencyContactServiceImpl.class);

    @Autowired
    private EmergencyContactFeignService emergencyContactFeignService;

    @Override
    public void saveEmergencyContact(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("请求参数===>" + body);
        RdEmergencyContactDTO params = JsonUtil.jsonToBean(body,RdEmergencyContactDTO.class);
        Integer userId = Integer.parseInt(httpServletRequest.getHeader("userId"));
        //校验参数
        if(StringUtil.isBlank(userId) || params == null) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"userId不能为空");
        }
        params.setUserId(userId);
        //TODO 对重要数据进行校验
        Util.sendResponse(emergencyContactFeignService.saveEmergencyContact(params),httpServletResponse);
    }

    @Override
    public void findEmergencyContactByUser(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("请求参数===>" + body);
        Integer userId = Integer.parseInt(httpServletRequest.getHeader("userId"));
        //校验参数
        if(StringUtil.isBlank(userId)) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"userId不能为空");
        }
        RdEmergencyContactDTO params = new RdEmergencyContactDTO();
        params.setUserId(userId);
        Util.sendResponse(emergencyContactFeignService.getByUser(params),httpServletResponse);
    }
}
